<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://isofttime.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\printer\model;

use think\Model;
use app\we\sdk\Thinkwechat;


class PrinterFans extends Model
{
    protected $autoWriteTimestamp = true;
    // 根据openid 获取用户
    public function info($key, $field = true)
    {
        
        if (!$key) return false;
        if (is_numeric($key)){
            $map['id'] = $key;
        }else{
            $map['openid'] = $key;
        }

        $fans=$this->field($field)->where($map)->find();

        return $fans;
    }

    

    // 更新用户
    public function checkFans($openid,$printer)
    {
       
        $cur_date = strtotime(date('Y-m-d',time()));
        $map['openid']=$openid;
        $fans = $this->where($map)->find();
        if (!$fans){
            $fans = model('WeFans')->info($openid);
         
            $data['openid']=$openid;
            $data['nickname']=$fans['nickname'];
            $data['balance']=$printer['free'];
            $data['aid']=$fans['aid'];
            $data['update_time']=time();
            $this->save($data);

        }elseif($fans['update_time']<$cur_date){
            //自动赠送一张
            $this->where($map)->setInc('balance',$printer['free']);
            $this->where($map)->setField('update_time',time());
          

        }

        $balance=$this->where($map)->value('balance');  //TODO::可以增加黑名单功能，被限制的用户永远返回0
        
        if ($balance>=1){
           $res= $this->where($map)->setInc('times');
           $res= $this->where($map)->setDec('balance');
        }

         return $balance-1; 
        
    }

     public function editData(){

      $data=input("post.");
      
      if ($data['id']){
         $res = $this->allowField(true)->where('id',$data['id'])->update($data);
      }else{
         $res = $this->allowField(true)->data($data)->save();
      }
      
      return $res;
    }

    public function pay($openid,$um){

        $map['openid']=$openid;
        $fans = $this->where($map)->find();
        if (!$fans){
            $fans = model('WeFans')->info($openid);
         
            $data['openid']=$openid;
            $data['nickname']=$fans['nickname'];
            $data['balance']=$um;
            $data['aid']=$fans['aid'];
            $data['update_time']=time();
            $this->save($data);

        }else{
            $this->where($map)->setInc('balance',$um);
            $this->where($map)->setField('update_time',time());
        }

        $balance=$this->where($map)->value('balance');  
      
      return $balance;
    }

   
}